package com.biodiv.server;

import com.biodiv.common.protocol.MessageCodecSharable;
import com.biodiv.common.protocol.ProcotolFrameDecoder;
import com.biodiv.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

import static com.biodiv.server.config.ServerConfig.SERVER_PORT;
import static com.biodiv.server.config.ServerConfig.printIcon;

/**
 * @Author: tianquan
 * @CreateTime: 2024-11-03  09:01
 * @Description: 注册中心启动类
 */

@Slf4j
public class PBankServer {

    public static void main(String[] args) {
        // 打印图标
        printIcon();
        // 创建处理连接的事件组
        NioEventLoopGroup boss = new NioEventLoopGroup();
        // 创建处理读写的事件组
        NioEventLoopGroup worker = new NioEventLoopGroup();
        // 服务端处理器
        ServerChannelInboundHandlerAdapterHandler SERVER_INBOUND_HANDLER = new ServerChannelInboundHandlerAdapterHandler();
        // 日志处理器
        LoggingHandler LOGGING_HANDLER = new LoggingHandler(LogLevel.DEBUG);
        // 消息编解码器
        MessageCodecSharable MESSAGE_CODEC = new MessageCodecSharable();
        // 登录验证事件处理器
        LoginRequestMessageHandler LOGIN_HANDLER = new LoginRequestMessageHandler();
        // 服务注册事件处理器
        ServiceRegisterMessageHandler SERVICE_REGISTR_HANDLER = new ServiceRegisterMessageHandler();
        // 服务获取事件处理器
        PullServiceRequestMessageHandler PULL_REQUEST_HANDLER = new PullServiceRequestMessageHandler();
        // 删除服务事件处理器
        DeleteInstanceMessageHandler DELETE_INSTANCE_HANDLER = new DeleteInstanceMessageHandler();

        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.channel(NioServerSocketChannel.class);
            serverBootstrap.group(boss, worker);
            serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
                @Override
                protected void initChannel(SocketChannel ch) throws Exception {
                    ch.pipeline().addLast(new ProcotolFrameDecoder());
                    // ch.pipeline().addLast(LOGGING_HANDLER);
                    ch.pipeline().addLast(MESSAGE_CODEC);
                    ch.pipeline().addLast(DELETE_INSTANCE_HANDLER);
                    ch.pipeline().addLast(LOGIN_HANDLER);
                    ch.pipeline().addLast(SERVICE_REGISTR_HANDLER);
                    ch.pipeline().addLast(PULL_REQUEST_HANDLER);
                    ch.pipeline().addLast(SERVER_INBOUND_HANDLER);
                }
            });
            Channel channel = serverBootstrap.bind(SERVER_PORT).sync().channel();
            channel.closeFuture().sync();
        } catch (InterruptedException e) {
            log.error("server error", e);
        } finally {
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }

}
